package it.appcatalogobase;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;

import android.app.FragmentManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class DoveSiamoFragment extends Fragment {
	DoveSiamoDataSource dataDS;
	SharedPreferences prefs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        prefs = getActivity().getSharedPreferences("it.appcatalogobase", Context.MODE_PRIVATE);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_dovesiamo, container, false);

       
        return rootView;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState){
    	super.onActivityCreated(savedInstanceState); 
    		 GoogleMap googleMap;
    		 googleMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
    		dataDS = new DoveSiamoDataSource(getActivity());
         	dataDS.open();
 	        List<DoveSiamo> lista = dataDS.getAllDoveSiamo();
			TextView txt = (TextView) getActivity().findViewById(R.id.textViewInd);
			TextView txtC = (TextView) getActivity().findViewById(R.id.textViewCell);
 			TextView txtM = (TextView) getActivity().findViewById(R.id.textViewMail);
 			TextView txtF = (TextView) getActivity().findViewById(R.id.textViewFax);
 			TextView txtT = (TextView) getActivity().findViewById(R.id.textViewTel);

 			if(lista.size()!=0){
	 	        try {
	 				String ind = URLDecoder.decode(Html.fromHtml(lista.get(0).getIndirizzo()).toString(), "ISO-8859-1");
	 				if(ind!=null && !ind.equals("") && !ind.equals("null"))
	 					txt.setText("Indirizzo: "+ind);
	 				else
	 					txt.setVisibility(View.GONE);
	 				String cell = URLDecoder.decode(Html.fromHtml(lista.get(0).getCell()).toString(), "ISO-8859-1");
	 				if(cell!=null && !cell.equals("") && !cell.equals("null"))
	 					txtC.setText("Cell: "+cell);
	 				else
	 					txtC.setVisibility(View.GONE);
	 				String tel = URLDecoder.decode(Html.fromHtml(lista.get(0).getTel()).toString(), "ISO-8859-1");
	 				if(tel!=null && !tel.equals("") && !tel.equals("null"))
	 					txtT.setText("Tel: "+tel);
	 				else
	 					txtT.setVisibility(View.GONE);
	 				String mail = URLDecoder.decode(Html.fromHtml(lista.get(0).getMail()).toString(), "ISO-8859-1");
	 				if(mail!=null && !mail.equals("") && !mail.equals("null"))
	 					txtM.setText("Mail: "+mail);
	 				else
	 					txtM.setVisibility(View.GONE);
	 				String fax = URLDecoder.decode(Html.fromHtml(lista.get(0).getFax()).toString(), "ISO-8859-1");
	 				if(fax!=null && !fax.equals("") && !fax.equals("null"))
	 					txtF.setText("Fax: "+fax);
	 				else
	 					txtF.setVisibility(View.GONE);
	 				
	 				String lat = URLDecoder.decode(Html.fromHtml(lista.get(0).getLat()).toString(), "ISO-8859-1");
	 				String lon = URLDecoder.decode(Html.fromHtml(lista.get(0).getLon()).toString(), "ISO-8859-1");
	 			    if(!lat.isEmpty() && !lat.equals("") && !lat.equals("null") && !lon.isEmpty() && !lon.equals("") && !lon.equals("null") && googleMap!=null){
	 			    	googleMap.addMarker(new MarkerOptions().position(new LatLng(Double.valueOf(lat), Double.valueOf(lon))));
	 			    	googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.valueOf(lat),Double.valueOf(lon)), 10.0f));
	 			    }else{
	 			    	Toast.makeText(getActivity(), "NON E' STATO POSSIBILE DECODIFICARE LE COORDINATE", Toast.LENGTH_LONG).show();
	 			    }

	 	        } catch (UnsupportedEncodingException e) {
	 				// TODO Auto-generated catch block
	 				e.printStackTrace();
	 			}
 			}else{
 				txt.setVisibility(View.GONE);
 				txtC.setVisibility(View.GONE);
 				txtT.setVisibility(View.GONE);
 				txtM.setVisibility(View.GONE);
 				txtF.setVisibility(View.GONE);
				Toast.makeText(getActivity(),"NON CI SONO DATI DISPONIBILI", Toast.LENGTH_LONG).show();
 			}
 			dataDS.close();
         
    }
    
    @Override
    public void onDestroyView() {
    	super.onDestroyView();
       FragmentManager fm = getActivity().getFragmentManager();

        android.app.Fragment xmlFragment = fm.findFragmentById(R.id.map);
        
        if (xmlFragment != null) {
            fm.beginTransaction().remove(xmlFragment).commit();
        }

        
    }

}